package com.huixi.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.huixi.common.pojo.SearchResult;
import com.huixi.entity.User;
import com.huixi.service.LoginService;
import com.huixi.service.SearchService;
import com.huixi.utils.JsonResult;

/**
 * 商品搜索Controller
 * 
 * @author 韦丽娜
 *
 */
@Controller
public class SearchController {

	@Autowired
	private SearchService searchService;

	@Autowired
	private LoginService loginService;

	/*
	 * @Value("${SEARCH_RESULT_ROWS}") private Integer SEARCH_RESULT_ROWS ;
	 */

	@RequestMapping("/search")
	public String searchItemList(String keyword, @RequestParam(defaultValue = "1") Integer page, Model model,
			HttpServletRequest request, HttpSession session) throws Exception {
		// keyword = new String (keyword.getBytes("ISO-8859-1"),"UTF-8");
		// 查询商品列表
		SearchResult searchResult = searchService.searchResult(keyword, page, 32);
		// 把结果传递给页面
		model.addAttribute("query", keyword);
		model.addAttribute("totalPages", searchResult.getTotalPages());
		model.addAttribute("recordCount", searchResult.getRecordCount());
		model.addAttribute("page", page);
		model.addAttribute("itemList", searchResult.getItemList());

		JsonResult jsonResult = loginService.getUserBySession(session);
		request.setAttribute("userInfo", (User) jsonResult.getData());
		// 返回逻辑视图
		return "search";
	}

}
